package br.inf.mfpo.fra4and.exemplo.provider;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import br.inf.mfpo.fra4and.exemplo.columns.CoisasColumns;
import br.inf.mfpo.fra4and.exemplo.columns.PessoasColumns;

public class DatabaseHelper extends DatabaseMetadata {

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.v(Fra4AndProvider.TAG, "Creating tables");
		db.execSQL(CREATE_TABLE_PESSOAS);
		db.execSQL(CREATE_TABLE_COISAS);

		Log.v(Fra4AndProvider.TAG, "Indexing tables...");
		db.execSQL(CREATE_IDX_PESSOAS_NAME);
		db.execSQL(CREATE_IDX_COISAS_NAME);

		Log.v(Fra4AndProvider.TAG, "Creating triggers...");
		db.execSQL(CREATE_TRG_DELETE_PESSOAS_COISAS);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public Uri insertPessoas(ContentValues values) {
		long rowId = getWritableDatabase().insert(PESSOAS_TABLE, PessoasColumns._ID, values);
		if (rowId > 0) {
			Uri uri = ContentUris.withAppendedId(PessoasColumns.CONTENT_URI, rowId);
			return uri;
		}

		throw new SQLiteException("Failed to insert row.");
	}

	public int deletePessoas(String selection, String[] selectionArgs) {
		return getWritableDatabase().delete(PESSOAS_TABLE, selection, selectionArgs);
	}

	public int deletePessoas(String id) {
		String where = PessoasColumns._ID + "=?";
		return deletePessoas(where, new String[] { id });
	}

	public int updatePessoas(ContentValues values, String where, String[] whereArgs) {
		return getWritableDatabase().update(PESSOAS_TABLE, values, where, whereArgs);
	}

	public int updatePessoas(String id, ContentValues values) {
		String where = PessoasColumns._ID + "=?";
		return updatePessoas(values, where, new String[] { id });
	}

	public Cursor queryPessoas(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(PESSOAS_TABLE);

		if (sortOrder == null || TextUtils.isEmpty(sortOrder)) {
			sortOrder = PessoasColumns.DEFAULT_SORT_ORDER;
		}

		Log.d(Fra4AndProvider.TAG, "Build query: " + builder.buildQuery(projection, selection, selectionArgs, null, null, sortOrder, null));

		return builder.query(getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
	}

	public Cursor queryPessoas(String id, String[] projection, String sortOrder) {
		String where = PessoasColumns._ID + "=?";

		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(PESSOAS_TABLE);
		builder.setDistinct(true);

		if (sortOrder == null || TextUtils.isEmpty(sortOrder)) {
			sortOrder = PessoasColumns.DEFAULT_SORT_ORDER;
		}

		Log.d(Fra4AndProvider.TAG, "Build query: " + builder.buildQuery(projection, where, new String[] { id }, null, null, sortOrder, null));

		return builder.query(getReadableDatabase(), projection, where, new String[] { id }, null, null, sortOrder);
	}

	public int deleteCoisas(String selection, String[] selectionArgs) {
		return getWritableDatabase().delete(COISAS_TABLE, selection, selectionArgs);
	}

	public int deleteCoisas(String id) {
		String where = CoisasColumns._ID + "=?";
		return deleteCoisas(where, new String[] { id });
	}

	public Uri insertCoisas(ContentValues values) {
		long rowId = getWritableDatabase().insert(COISAS_TABLE, CoisasColumns._ID, values);
		if (rowId > 0) {
			Uri uri = ContentUris.withAppendedId(CoisasColumns.CONTENT_URI, rowId);
			return uri;
		}

		throw new SQLiteException("Failed to insert row.");
	}

	public Cursor queryCoisas(String id, String[] projection, String sortOrder) {
		String where = CoisasColumns._ID + "=?";

		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(COISAS_TABLE);
		builder.setDistinct(true);

		if (sortOrder == null || TextUtils.isEmpty(sortOrder)) {
			sortOrder = CoisasColumns.DEFAULT_SORT_ORDER;
		}

		Log.d(Fra4AndProvider.TAG, "Build query: " + builder.buildQuery(projection, where, new String[] { id }, null, null, sortOrder, null));

		return builder.query(getReadableDatabase(), projection, where, new String[] { id }, null, null, sortOrder);
	}

	public Cursor queryCoisas(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(COISAS_TABLE);

		if (sortOrder == null || TextUtils.isEmpty(sortOrder)) {
			sortOrder = CoisasColumns.DEFAULT_SORT_ORDER;
		}

		Log.d(Fra4AndProvider.TAG, "Build query: " + builder.buildQuery(projection, selection, selectionArgs, null, null, sortOrder, null));

		return builder.query(getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
	}

	public int updateCoisas(ContentValues values, String where, String[] whereArgs) {
		return getWritableDatabase().update(COISAS_TABLE, values, where, whereArgs);
	}

	public int updateCoisas(String id, ContentValues values) {
		String where = CoisasColumns._ID + "=?";
		return updateCoisas(values, where, new String[] { id });
	}
}